package edu.upe.poli.dsc.sd.util;

public class StringUtil {

	public static final String LINE_BREAK_CHARACTER = new String("\n");
	public static final int LEFT_PADDING = 0;
	public static final int RIGHT_PADDING = 1;
	
	public static final String[][] fromTo = new String[][]{
														{"\n", "\\\\n"},
														{"\t", "\\\\t"},
														{"\\+", "\\\\m"},
														{"\\\\", "\\\\\\\\"},
													}; 
	
	public static String pad(String str, int length, String padder, int paddingOrientation){
		
		StringBuffer pad = new StringBuffer();
		
		while(str.length() + pad.length() < length) 
			pad.append(padder);
		
		if(paddingOrientation == LEFT_PADDING)
			return pad.append(str).toString();
		else 
			return str + pad.toString();
	}
	
	public static String getNumberedLine(String str, int number){
		
		return str.split(LINE_BREAK_CHARACTER, number+1)[number-1];
	}
	
	public static String encode(String str){
		
		for (int i = 0; i < fromTo.length; i++)
			str = str.replaceAll(fromTo[i][0], fromTo[i][1]);
		
		return str;
	}
	
	public static String decode(String str){

		for (int i = fromTo.length - 1; i >= 0 ; i--)
			str = str.replaceAll(fromTo[i][1], fromTo[i][0]);
		
		return str;
	}
	
	
}
